<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goodsDetailList">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodsDetailList.categories[1].id}`">{{
          goodsDetailList.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{
          goodsDetailList.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodsDetailList.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <XtxImageView :image-list="goodsDetailList.mainPictures" />
          <!-- 商品销售情况 -->
          <GoodsSale :goodsDetailList="goodsDetailList" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodsInfo :goodsDetailList="goodsDetailList" />
          <!-- 库存量组件 -->
          <XtxGoodSku :goodsDetailList="goodsDetailList"  @change="goodSkuChangeFn" />
          <!--  :max='getgoodsDetail.inventory'-->
          <XtxNumbox v-model="count" :min='1' />
           <XtxButton type="primary" style="margin-top:20px" @click="addCart">添加购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
              <GoodsDetail :goodsDetailList="goodsDetailList"></GoodsDetail>
          </div>

        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
            <GoodsHot :type='1' :goodsId='goodsDetailList.id'></GoodsHot>
            <GoodsHot :type='2' :goodsId='goodsDetailList.id'></GoodsHot>
            <GoodsHot :type='3' :goodsId='goodsDetailList.id'></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getgoodsDetail } from '@/api/goods'
import { useRoute } from 'vue-router'
import { ref } from 'vue'
import GoodsSale from '@/views/Goods/components/goods-sale.vue'
import GoodsInfo from '@/views/Goods/components/goods-info.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import { useStore } from 'vuex'
import { message } from '@/components/Message'
export default {
  components: { GoodsSale, GoodsInfo, GoodsDetail, GoodsHot },
  name: 'XtxGoodsPage',
  setup () {
    const route = useRoute()
    const goodsDetailList = ref(null)
    // 发送请求获取商品详情数据
    getgoodsDetail(route.params.id).then(({ result }) => {
      // console.log(result)
      goodsDetailList.value = result
    })
    const goodSkuChangeFn = (val) => {
      // console.log(val)
      if (val.skuId) {
        // goods.value.price = val.price
        // goods.value.oldPrice = val.oldPrice
        // goods.value.inventory = val.inventory
        // goods.value.skuId = val.skuId
        // goods.value.specsText = val.specsText
        // 🔔 通过遍历对象化简代码
        for (const key in val) {
          goodsDetailList.value[key] = val[key]
        }
      }
    }
    const count = ref(1)
    const store = useStore()
    const addCart = () => {
      // 判断用户是否有选择规格
      // console.log(12333333)
      if (!goodsDetailList.value.skuId) {
        message({ text: '请选择商品规格' })
        return
      }
      // 判定选择商品不能超过库存
      if (count.value > goodsDetailList.value.inventory) return message({ text: '超出库存啦~' })
      const goodsObj = {
        id: goodsDetailList.value.id,
        name: goodsDetailList.value.name,
        picture: goodsDetailList.value.mainPictures[0],
        skuId: goodsDetailList.value.skuId,
        price: goodsDetailList.value.oldPrice,
        nowPrice: goodsDetailList.value.price,
        attrsText: goodsDetailList.value,
        stock: goodsDetailList.value.inventory,
        isEffective: true,
        selected: true,
        count: count.value
      }
      store.dispatch('cart/asyncInertCart', goodsObj).then(() => {
        message({ type: 'success', text: '添加购物车成功！' })
      })
      // console.log(goodsDetailList.value)
      // console.log(goodsObj)
    }

    return { goodsDetailList, goodSkuChangeFn, count, addCart }
  }
}
</script>
<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;

  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
